package org.seven.jrdp.web.t;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.seven.jrdp.biz.t.service.TKqService;
import org.seven.jrdp.commons.util.Excel2007Utils;
import org.seven.jrdp.commons.util.ServletUtils;
import org.seven.jrdp.web.t.plugins.ExcelPlugin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("/mgt/t/kcmx")
public class TKcmxController {
	private static final String EXCEL_KCMX = "kcmx.xlsx";
	private static final int LEN_KQ = 22;
	@Autowired
	private TKqService tKqService;

	@GetMapping("/{view}")
	public ModelAndView view(@PathVariable String view) {
		return new ModelAndView("mgt/t/kcmx/kcmx_" + view);
	}

	@PostMapping("/find")
	public List<Map<String, Object>> find(HttpServletRequest request) {
		Map<String, Object> map = ServletUtils.getSearchParams(request);
		return tKqService.findKcmx(map);
	}

	@RequestMapping("/dc")
	public void dc(HttpServletRequest request, HttpServletResponse response) {
		List<Map<String, Object>> list = tKqService.findDc();
		Workbook workbook = Excel2007Utils.getWorkbook(ServletUtils.getExcelPath(EXCEL_KCMX));
		dc(workbook, list);
		String fileName = "库存明细" + Excel2007Utils.SUFFIX;
		ServletUtils.download(workbook, fileName, request, response);
	}

	@SuppressWarnings("unchecked")
	private void dc(Workbook workbook, List<Map<String, Object>> list) {
		ExcelPlugin plugin = new ExcelPlugin(workbook);
		Sheet sheet = workbook.getSheetAt(0);
		int index = 0;
		for (Map<String, Object> map : list) {
			List<Map<String, Object>> hz = (List<Map<String, Object>>) map.get("hz");
			for (int i = 0; i < (Integer) map.get("sl"); i += 4) {
				Row row = sheet.createRow(index);
				row.setHeightInPoints(20f);
				Row row1 = sheet.createRow(index + 1);
				row1.setHeightInPoints(20f);
				for (int j = 0; j < 21; j++) {
					row.createCell(j).setCellStyle(plugin.STYLE_FONT10_BOLD);
					row1.createCell(j).setCellStyle(plugin.STYLE_FONT10);
				}
				row.getCell(0).setCellValue("产品名称");
				row.getCell(5).setCellValue((String) map.get("ggxh"));
				row.getCell(10).setCellValue("产品编号");
				row.getCell(15).setCellValue((String) map.get("cpbh"));
				row.getCell(20).setCellValue("库区位");
				sheet.addMergedRegion(new CellRangeAddress(index, index, 0, 4));
				sheet.addMergedRegion(new CellRangeAddress(index, index, 5, 9));
				sheet.addMergedRegion(new CellRangeAddress(index, index, 10, 14));
				sheet.addMergedRegion(new CellRangeAddress(index, index, 15, 19));
				sheet.addMergedRegion(new CellRangeAddress(index, index + 1, 20, 20));
				row1.getCell(0).setCellValue("库区位");
				row1.getCell(1).setCellValue("炉次号");
				row1.getCell(2).setCellValue("序列号");
				row1.getCell(3).setCellValue("每困重量");
				row1.getCell(4).setCellValue("块数");
				row1.getCell(5).setCellValue("库区位");
				row1.getCell(6).setCellValue("炉次号");
				row1.getCell(7).setCellValue("序列号");
				row1.getCell(8).setCellValue("每困重量");
				row1.getCell(9).setCellValue("块数");
				row1.getCell(10).setCellValue("库区位");
				row1.getCell(11).setCellValue("炉次号");
				row1.getCell(12).setCellValue("序列号");
				row1.getCell(13).setCellValue("每困重量");
				row1.getCell(14).setCellValue("块数");
				row1.getCell(15).setCellValue("库区位");
				row1.getCell(16).setCellValue("炉次号");
				row1.getCell(17).setCellValue("序列号");
				row1.getCell(18).setCellValue("每困重量");
				row1.getCell(19).setCellValue("块数");
				for (int j = 0; j <= LEN_KQ; j++) {
					Row row2 = sheet.createRow(index + 2 + j);
					row2.setHeightInPoints(20f);
					for (int k = 0; k < 21; k++) {
						row2.createCell(k).setCellStyle(plugin.STYLE_FONT10);
					}
				}
				Row row3 = sheet.createRow(index + 2 + LEN_KQ);
				row3.setHeightInPoints(20f);
				for (int j = 0; j < 21; j++) {
					row3.createCell(j).setCellStyle(plugin.STYLE_FONT10_BOLD);
				}
				row3.getCell(0).setCellValue("合计1：");
				row3.getCell(5).setCellValue("合计2：");
				row3.getCell(10).setCellValue("合计3：");
				row3.getCell(15).setCellValue("合计4：");
				sheet.addMergedRegion(new CellRangeAddress(index + 2 + LEN_KQ, index + 2 + LEN_KQ, 0, 1));
				sheet.addMergedRegion(new CellRangeAddress(index + 2 + LEN_KQ, index + 2 + LEN_KQ, 2, 4));
				sheet.addMergedRegion(new CellRangeAddress(index + 2 + LEN_KQ, index + 2 + LEN_KQ, 5, 6));
				sheet.addMergedRegion(new CellRangeAddress(index + 2 + LEN_KQ, index + 2 + LEN_KQ, 7, 9));
				sheet.addMergedRegion(new CellRangeAddress(index + 2 + LEN_KQ, index + 2 + LEN_KQ, 10, 11));
				sheet.addMergedRegion(new CellRangeAddress(index + 2 + LEN_KQ, index + 2 + LEN_KQ, 12, 14));
				sheet.addMergedRegion(new CellRangeAddress(index + 2 + LEN_KQ, index + 2 + LEN_KQ, 15, 16));
				sheet.addMergedRegion(new CellRangeAddress(index + 2 + LEN_KQ, index + 2 + LEN_KQ, 17, 19));
				for (int j = index + 2; j < index + 2 + LEN_KQ; j++) {
					sheet.getRow(j).getCell(20).setCellStyle(plugin.STYLE_FONT10_BOLD);
				}
				sheet.addMergedRegion(new CellRangeAddress(index + 2, index + 6, 20, 20));
				sheet.getRow(index + 7).getCell(20).setCellValue(new XSSFRichTextString("捆数\n合计"));
				sheet.addMergedRegion(new CellRangeAddress(index + 7, index + 9, 20, 20));
				sheet.addMergedRegion(new CellRangeAddress(index + 10, index + 12, 20, 20));
				sheet.getRow(index + 13).getCell(20).setCellValue(new XSSFRichTextString("重量\n合计\n(kg)"));
				sheet.addMergedRegion(new CellRangeAddress(index + 13, index + 16, 20, 20));
				sheet.addMergedRegion(new CellRangeAddress(index + 17, index + 2 + LEN_KQ, 20, 20));
				int zl = 0;
				int ks = 0;
				List<String> kqmc = new ArrayList<String>();
				if (hz.size() > i) {
					Map<String, Object> hz1 = hz.get(i);
					row3.getCell(2).setCellValue((Integer) hz1.get("zl"));
					zl += (Integer) hz1.get("zl");
					kqmc.add((String) hz1.get("kqmc"));
					List<Map<String, Object>> mx = (List<Map<String, Object>>) hz1.get("mx");
					ks += mx.size();
					for (int jj = 0; jj < mx.size(); jj++) {
						Map<String, Object> mx1 = mx.get(jj);
						sheet.getRow(index + 2 + jj).getCell(0).setCellValue((String) mx1.get("kqmc"));
						sheet.getRow(index + 2 + jj).getCell(1).setCellValue((String) mx1.get("cpbh"));
						sheet.getRow(index + 2 + jj).getCell(2).setCellValue((String) mx1.get("cpxh"));
						sheet.getRow(index + 2 + jj).getCell(3).setCellValue((Integer) mx1.get("zl"));
						sheet.getRow(index + 2 + jj).getCell(4).setCellValue((Integer) mx1.get("ks"));
					}
				}
				if (hz.size() > i + 1) {
					Map<String, Object> hz2 = hz.get(i + 1);
					row3.getCell(7).setCellValue((Integer) hz2.get("zl"));
					zl += (Integer) hz2.get("zl");
					kqmc.add((String) hz2.get("kqmc"));
					List<Map<String, Object>> mx = (List<Map<String, Object>>) hz2.get("mx");
					ks += mx.size();
					for (int jj = 0; jj < mx.size(); jj++) {
						Map<String, Object> mx1 = mx.get(jj);
						sheet.getRow(index + 2 + jj).getCell(5).setCellValue((String) mx1.get("kqmc"));
						sheet.getRow(index + 2 + jj).getCell(6).setCellValue((String) mx1.get("cpbh"));
						sheet.getRow(index + 2 + jj).getCell(7).setCellValue((String) mx1.get("cpxh"));
						sheet.getRow(index + 2 + jj).getCell(8).setCellValue((Integer) mx1.get("zl"));
						sheet.getRow(index + 2 + jj).getCell(9).setCellValue((Integer) mx1.get("ks"));
					}
				}
				if (hz.size() > i + 2) {
					Map<String, Object> hz3 = hz.get(i + 2);
					row3.getCell(12).setCellValue((Integer) hz3.get("zl"));
					zl += (Integer) hz3.get("zl");
					kqmc.add((String) hz3.get("kqmc"));
					List<Map<String, Object>> mx = (List<Map<String, Object>>) hz3.get("mx");
					ks += mx.size();
					for (int jj = 0; jj < mx.size(); jj++) {
						Map<String, Object> mx1 = mx.get(jj);
						sheet.getRow(index + 2 + jj).getCell(10).setCellValue((String) mx1.get("kqmc"));
						sheet.getRow(index + 2 + jj).getCell(11).setCellValue((String) mx1.get("cpbh"));
						sheet.getRow(index + 2 + jj).getCell(12).setCellValue((String) mx1.get("cpxh"));
						sheet.getRow(index + 2 + jj).getCell(13).setCellValue((Integer) mx1.get("zl"));
						sheet.getRow(index + 2 + jj).getCell(14).setCellValue((Integer) mx1.get("ks"));
					}
				}
				if (hz.size() > i + 3) {
					Map<String, Object> hz4 = hz.get(i + 3);
					row3.getCell(17).setCellValue((Integer) hz4.get("zl"));
					zl += (Integer) hz4.get("zl");
					kqmc.add((String) hz4.get("kqmc"));
					List<Map<String, Object>> mx = (List<Map<String, Object>>) hz4.get("mx");
					ks += mx.size();
					for (int jj = 0; jj < mx.size(); jj++) {
						Map<String, Object> mx1 = mx.get(jj);
						sheet.getRow(index + 2 + jj).getCell(15).setCellValue((String) mx1.get("kqmc"));
						sheet.getRow(index + 2 + jj).getCell(16).setCellValue((String) mx1.get("cpbh"));
						sheet.getRow(index + 2 + jj).getCell(17).setCellValue((String) mx1.get("cpxh"));
						sheet.getRow(index + 2 + jj).getCell(18).setCellValue((Integer) mx1.get("zl"));
						sheet.getRow(index + 2 + jj).getCell(19).setCellValue((Integer) mx1.get("ks"));
					}
				}
				sheet.getRow(index + 2).getCell(20).setCellValue(String.join(",", kqmc));
				sheet.getRow(index + 10).getCell(20).setCellValue(ks);
				sheet.getRow(index + 17).getCell(20).setCellValue(zl);
				index += 3 + LEN_KQ;
			}
		}
	}
}